package com.rain.shoppingscienceproj.ui.activity.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.adapter.SearchAdapter;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.SharedPreferencesUtil;
import com.rain.shoppingscienceproj.utils.layoutmanager.CusGridLayoutManager;
import com.rain.shoppingscienceproj.utils.layoutmanager.item.SpaceItemDecoration;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchLocalityActivity extends BaseActivity {


    @OnClick(R.id.rl_back)
    void back() {
        finish();
    }

    @BindView(R.id.rv_locality_list)
    RecyclerView rv_locality_list;

    @BindView(R.id.et_search)
    EditText et_search;

    @BindView(R.id.tv_cancel)
    TextView tv_cancel;

    private SearchAdapter localityAdapter;
    private List<String> localityList = new ArrayList<>();
    private String queryString = "";
    private Gson gson = new Gson();


    private int type;//首页是1/省份进入2
    private String provinceId, typeId;//省份ID 分类id


    @OnClick(R.id.rl_del)
    void del() {
        localityList.clear();
        SharedPreferencesUtil.putString(this, "localitySearch", "localitySearch", "");
        localityAdapter.setLocalityList(null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_locality);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        provinceId = getIntent().getStringExtra("provinceId");
        typeId = getIntent().getStringExtra("typeId");
        initView();
        initListener();
    }

    private void initView() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        String localityString = SharedPreferencesUtil.getString(this, "localitySearch", "localitySearch", "");
        rv_locality_list.setLayoutManager(new CusGridLayoutManager(this, 5));
        rv_locality_list.addItemDecoration(new SpaceItemDecoration(10));
        localityAdapter = new SearchAdapter(this);
        rv_locality_list.setAdapter(localityAdapter);
        if (localityString != null && !localityString.equals("")) {
            localityList = gson.fromJson(localityString, new TypeToken<List<String>>() {
            }.getType());
            localityAdapter.setLocalityList(localityList);
        }
    }

    @OnClick(R.id.tv_cancel)
    void search() {
        String text = et_search.getText().toString().trim();

        if (text.isEmpty()) {
            ToastUtils.showShortToast("请输入搜索的商品");
        } else {
            localityList.add(text);
            SharedPreferencesUtil.putString(SearchLocalityActivity.this, "localitySearch", "localitySearch", gson.toJson(localityList));
            localityAdapter.setLocalityList(localityList);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(tv_cancel.getWindowToken(), 0);
            startActivitys(text);
        }
    }


    private void initListener() {
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    queryString = v.getText().toString().trim();
                    if (queryString.isEmpty()) {
                        ToastUtils.showShortToast("请输入搜索的商品");
                    } else {
                        localityList.add(queryString);
                        SharedPreferencesUtil.putString(SearchLocalityActivity.this, "localitySearch", "localitySearch", gson.toJson(localityList));
                        localityAdapter.setLocalityList(localityList);
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        startActivitys(queryString);
                    }
                    return false;
                }
                return false;
            }
        });
        localityAdapter.setItemListenerString(title -> {
            startActivitys(title);
        });
    }

    /**
     * 查询
     *
     * @param queryString
     */
    public void startActivitys(String queryString) {
        Intent intent = new Intent(SearchLocalityActivity.this, SearchActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("searchString", queryString);
        if (type == 2) {
            intent.putExtra("provinceId", provinceId);
        } else if (type == 3) {
            intent.putExtra("provinceId", provinceId);
            intent.putExtra("typeId", typeId);
        }
        startActivity(intent);
    }
}
